package com.chx.three.mama.fragments;


import android.content.Intent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.chx.three.mama.R;
import com.chx.three.mama.activities.SettingActivity;
import com.chx.three.mama.view.Topbar;
import com.chx.three.mama.activities.MineWebViewActivity;
import com.chx.three.mama.activities.PersonMessageActivity;
import com.chx.three.mama.activities.ProblemHelpActivity;
import com.chx.three.mama.model.MineItem;
import com.chx.three.mama.model.RingItem;
import com.chx.three.mama.utils.BitmapHelper;
import com.chx.three.mama.utils.Constants;
import com.chx.three.mama.utils.SPHelper;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * 我的模块的fragment
 */
public class MineFragment extends Fragment implements View.OnClickListener,Topbar.onTopbarClickListener {
    public static final String TAG = "MineFragment";
    /**
     * 请求队列，用于发送网络请求
     */
    private RequestQueue requestQueue;

    private MineItem mineItem;
    private TextView mLevel;
    private TextView mAgeBorn;
    private TextView mCity;
    private TextView mWeekReplyNum;
    private TextView mCoinNum;
    private TextView mOnlineDaysNum;
    private TextView mQuanRank;
    private TextView mPostNum;
    private TextView mCollectNum;
    private TextView mReplyNum;
    private ImageView mHeadImg;

    public MineFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BitmapHelper.initBitmapHelper(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_mine, container, false);
        ((Topbar)ret.findViewById(R.id.topbar)).setOnTopbarClickListener(this);

        mineItem = new MineItem();

        //初始化样本数据
        initData();

        RelativeLayout mPersonMsg = (RelativeLayout) ret.findViewById(R.id.person_mess);
        RelativeLayout mCoin = (RelativeLayout) ret.findViewById(R.id.my_coin_part);
        RelativeLayout mOnlineDay = (RelativeLayout) ret.findViewById(R.id.online_days_part);
        mPersonMsg.setOnClickListener(this);
        mCoin.setOnClickListener(this);
        mOnlineDay.setOnClickListener(this);

        mHeadImg = (ImageView) ret.findViewById(R.id.show_head);

        mLevel = (TextView) ret.findViewById(R.id.my_level);


        mAgeBorn = (TextView) ret.findViewById(R.id.age_str);

        mCity = (TextView) ret.findViewById(R.id.city);

        mWeekReplyNum = (TextView) ret.findViewById(R.id.my_week_reply_num);

        mCoinNum = (TextView) ret.findViewById(R.id.my_coin_num);

        mOnlineDaysNum = (TextView) ret.findViewById(R.id.online_days_num);

        mQuanRank = (TextView) ret.findViewById(R.id.my_quan_rank);

        mPostNum = (TextView) ret.findViewById(R.id.my_post_num);

        mCollectNum = (TextView) ret.findViewById(R.id.my_collect_num);

        mReplyNum = (TextView) ret.findViewById(R.id.my_reply_num);

        TextView mProblem = (TextView) ret.findViewById(R.id.help_btn);
        mProblem.setOnClickListener(this);

        return ret;
    }

    @Override
    public void onLeftButtonClick() {

    }

    @Override
    public void onRightButtonClick() {
        Intent intent=new Intent(getActivity(), SettingActivity.class);
        startActivity(intent);
    }

    @Override
    public void onSearchButtonClick() {

    }

    public void initData() {
        requestQueue = Volley.newRequestQueue(getActivity());
        String url = Constants.URL.MINE_URL;
        JsonObjectRequest request = new JsonObjectRequest(url,
                new JSONObject(),
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject object) {
                        mineItem = JSON.parseObject(object.toString(), MineItem.class);

                        String imgUrl = mineItem.getHead();
                        Log.d(TAG, "object:" + imgUrl);
                        BitmapHelper.getBitmapUtils().display(
                                mHeadImg,
                                imgUrl,
                                new BitmapLoadCallBack<ImageView>() {
                                    @Override
                                    public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                                        int width = bitmap.getWidth();
                                        Bitmap dst = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
                                        Paint paint = new Paint();
                                        paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
                                        Canvas canvas = new Canvas(dst);
                                        canvas.drawCircle(width / 2, width / 2, width / 2, paint);
                                        imageView.setImageBitmap(dst);
                                    }

                                    @Override
                                    public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                                        imageView.setImageDrawable(drawable);
                                    }
                                }
                        );

                        String level = mineItem.getLevel();
                        level = "LV." + level;
                        mLevel.setText(level);
                        mAgeBorn.setText(mineItem.getAge_str());
                        mCity.setText(mineItem.getCity());
                        mWeekReplyNum.setText(mineItem.getWeek_reply());
                        mCoinNum.setText(mineItem.getCoin());
                        mOnlineDaysNum.setText(mineItem.getAct_day());
                        mQuanRank.setText(mineItem.getQuan_rank());
                        mPostNum.setText(mineItem.getPost_num());
                        mCollectNum.setText(mineItem.getCollect());
                        mReplyNum.setText(mineItem.getReply_num());

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(getActivity(), "未能获取到数据>.<~", Toast.LENGTH_SHORT).show();
                    }
                });
        requestQueue.add(request);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        Intent intent = null;
        String title;
        String url;
        switch (id){
            case R.id.help_btn:
                intent = new Intent(getActivity(), ProblemHelpActivity.class);
                url = "http://baby.ci123.com/yunqi/ios2/problem.php";
                intent.putExtra("url",url);
                break;
            case R.id.person_mess:
                intent = new Intent(getActivity(), PersonMessageActivity.class);
                break;
            case R.id.my_coin_part:
                title = "用户积分规则";
                url = "http://baby.ci123.com/yunqi/android5/describe.php";
                intent = new Intent(getActivity(), MineWebViewActivity.class);
                intent.putExtra("url",url);
                intent.putExtra("title",title);
                break;
            case R.id.online_days_part:
                title = "活跃天数";
                url = "http://baby.ci123.com/yunqi/android5/active.php";
                intent = new Intent(getActivity(), MineWebViewActivity.class);
                intent.putExtra("url",url);
                intent.putExtra("title",title);
                break;
        }
        if(intent != null){
            startActivity(intent);
        }
    }
}
